package com.jyw.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jyw.R;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.DuiHuan;
import com.jyw.utils.ScreenUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/12
 */

public class DuihuanHistoryActivity extends BaseActivity {
    private RecyclerView recycler;
    private DuiHuanAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duihuan_history);

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(DuihuanHistoryActivity.this));
        recycler.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(this)
                        .color(Color.TRANSPARENT)
                        .sizeResId(R.dimen.divider)
                        .build());
        recycler.setAdapter(adapter = new DuiHuanAdapter());

        loadData();
    }

    private void loadData() {
        Request request = new Request.Builder().
                url(Server.GET_INT_LOG + "?phone_data=" + getPrefs().getString("phone", "")).
                build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(DuihuanHistoryActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                JSONArray arr;
                final List<DuiHuan> orders = new ArrayList<>();
                try {
                    arr = new JSONArray(response.body().string());
                    if (arr.length() != 0) {
                        for (int i = 0; i < arr.length(); i++) {
                            JSONObject o = arr.getJSONObject(i);
                            orders.add(new DuiHuan(o.getString("id")
                                    , o.getString("phone")
                                    , o.getString("item_id")
                                    , o.getString("int")
                                    , o.getString("address_id")
                                    , o.getString("state")
                                    , o.getString("time")
                                    , o.getString("address")));
                        }

                        new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected Void doInBackground(Void... params) {
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void aVoid) {
                                super.onPostExecute(aVoid);
                                adapter.swapData(orders);
                            }
                        }.execute();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    class DuiHuanAdapter extends RecyclerView.Adapter<DuiHuanAdapter.DuiHuanHolder> {
        private List<DuiHuan> data = new ArrayList<>();

        void swapData(List<DuiHuan> data0) {
            this.data = data0;
            notifyDataSetChanged();
        }

        @Override
        public DuiHuanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new DuiHuanHolder(LayoutInflater.from(DuihuanHistoryActivity.this).inflate(R.layout.item_duihuan, null));
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(DuiHuanHolder holder, int position) {
            holder.tvSP.setText("兑换商品：20160219-001-010");
            holder.tvTime.setText("兑换时间：" + data.get(position).getTime());
            holder.tvAddress.setText("收货地址：" + data.get(position).getAddress());
            holder.tvState.setText("发货中...");
            holder.tvAddress.setMinWidth(ScreenUtils.getScreenWidth(DuihuanHistoryActivity.this));
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class DuiHuanHolder extends RecyclerView.ViewHolder {
            TextView tvSP, tvTime, tvAddress, tvState;

            DuiHuanHolder(View itemView) {
                super(itemView);
                tvSP = (TextView) itemView.findViewById(R.id.tv_sp);
                tvTime = (TextView) itemView.findViewById(R.id.tv_time);
                tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
                tvState = (TextView) itemView.findViewById(R.id.tv_state);
            }
        }
    }


}
